import type { RouteLocationNormalizedGeneric } from "vue-router";
import portService from "@/apis";
import useMenuStore from "@/pinia/menu";
import useUserStore from "@/pinia/user";

// 设置对应路由的浏览器标签标题
export const setBrowserTitle = (to: RouteLocationNormalizedGeneric) => {
  document.title = ("待办系统 - " + to.meta.title) as string;
};

// 获取菜单
export const getMenuList = (to: RouteLocationNormalizedGeneric) => {
  if (["/login", "/logout"].includes(to.path)) return;
  const menuStore = useMenuStore();
  if (menuStore.menuList.length) return false;
  menuStore.setMenuList();
};

export const checkUserInfo = async (to: RouteLocationNormalizedGeneric) => {
  if (["/login", "/logout"].includes(to.path)) return false;
  const userStore = useUserStore();
  if (!userStore.userInfo.id) {
    const { data, code } = await portService.userCheckApi();
    if (code === 0) {
      userStore.setUserInfo(data);
    }
  }
};
